package com.study.controller;

import com.study.pojo.User;
import com.study.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/", produces = "text/html;charset=UTF-8")
    public String index(Model model){
        model.addAttribute("msg","请登录！");
        return "index";
    }
    @PostMapping(value = "/login", produces = "text/html;charset=UTF-8")
    public String login(String username, String password, Model model, HttpSession session){
        System.out.println(username+password);
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        //调用Service判定密码是否错误
        boolean flag = userService.login(username, md5Password);
        model.addAttribute("username",username);
        if(flag){
            User user = userService.getUser(username);
            System.out.println(user.toString());

            session.setAttribute("user",user);
            return "index";
        }
        else{
            model.addAttribute("msg","账号或者密码错误");
            return "login";
        }
    }
    @GetMapping("/register")
    public String register(){
        return "register";
    }
    @GetMapping("/toLogin")
    public String toLogin(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
//        System.out.println(user);
        if(user!=null){
            model.addAttribute("msg",user.getUsername());
            return "myAccount";
        }
        return "/login";
    }
    @GetMapping("/toRegister")
    public String toRegister(){
        return "/register.jsp";
    }
    //注册接口
    @PostMapping("/registersuccess")
    public String registersuccess(String username, String password, String email, String repassword, String gender, String telephone, String introduce) {
        System.out.println(username+password+email+repassword+gender+telephone+introduce);
        int i = 0;
        if(password.equals(repassword)){
            //MD5加密
            String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
            System.out.println(md5Password);
             i= userService.logon(username, md5Password,email,gender,telephone,introduce);
        }

        if(i>=0){
//            model.addAttribute("msg","注册成功");
            //response.getWriter().write("<script>alert('注册成功!');window.location='adminshow'; </script>");
            return "index";
        }
        return "login";
    }
    @GetMapping("/toChangePWd")
    public String toChangePWd(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        model.addAttribute("user",user);
        return "modifyuserinfo";
    }
    @RequestMapping("/updatePwd")
    public String updatePwd(String password, Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        //System.out.println("session中"+username1);
        //System.out.println("前端"+username);
//        if(!username.equals(user.getUsername())){
//            System.out.println("没有权限");
//            model.addAttribute("msg","没有权限");
//            return "changePwd";
//        }
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        int i = userService.updatePwd(user.getUsername(), user.getPassword(),md5Password);
        if(i==0) {
            System.out.println("修改失败");
            model.addAttribute("msg","修改失败");
            return "changePwd";
        }else {
            model.addAttribute("msg","密码修改成功，请重新登录");
            //去除session
            session.removeAttribute("user");
            return "index";
        }
    }
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "index";
    }
    @GetMapping("/toHelp")
    public String toHelp(){
        return "baidu";
    }
}
